/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.stripes.hyperstripes.core.tags;

import com.farent.solutions.plugins.stripes.hyperstripes.core.Attribute;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentProcessor;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentTagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentTagHandlerFactory;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormChildComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormMode;
import com.farent.solutions.plugins.stripes.hyperstripes.core.StripesApplicationContext;
import com.farent.solutions.plugins.stripes.hyperstripes.core.TagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.utils.Utils;
import java.io.OutputStream;
import java.util.List;
import javax.xml.namespace.QName;
import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

/**
 *
 * @author user
 */
@TagHandler("FormRow")
public class FormRowTagHandler extends InputTagHandler implements ComponentTagHandler {

    @Override
    public String createContent(ComponentModel model, Object bindings) {
        StringBuilder builder = new StringBuilder();

        String labelTagName = "label";

        //get label value5322
        Attribute label = model.getAttribute(labelTagName);
        ComponentTagHandlerFactory tagFactory = ComponentTagHandlerFactory.getInstance();
        String id = Utils.EMPTY_STRING;

        StripesApplicationContext applicationContext =
                model.getComponentTypeStateHelper().getHsContext();

        if (label != null) {
            List<ComponentModel> children = model.getChildren();
            if (!Utils.nullOREmpty(children)) {
                FormChildComponentModel child = (FormChildComponentModel) children.get(0);
                id = child.getId();
            }

            StandardHTMLComponentModel labelModel = new StandardHTMLComponentModel(labelTagName);
            labelModel.addAttribute(new Attribute("for", id));
            labelModel.addAttribute(new Attribute("class", "hs-form-lbl"));
            labelModel.setText(label.getValue());
            //create label

            ComponentTagHandler labelTagHandler = tagFactory.getTagHandlerForTag( new QName(labelTagName), applicationContext);
            builder.append(labelTagHandler.createContent(labelModel, bindings));
        }

        //create input tag
        builder.append(this.processContent(model, bindings));

        //create div wrapping
        builder.trimToSize();

        String divTagName = "div";
        StandardHTMLComponentModel divModel = new StandardHTMLComponentModel(divTagName);
        divModel.addAttribute(new Attribute("class", "grid-12-12"));
        divModel.setText(builder.toString());
        ComponentTagHandler divTagHandler = tagFactory.getTagHandlerForTag(new QName(divTagName), applicationContext );

        return divTagHandler.createContent(divModel, bindings);
    }

    @Override
    String processContent(ComponentModel model, Object bindings) {
        FormRowComponentModel rowModel = (FormRowComponentModel) model;

        List<ComponentModel> children = rowModel.getChildren();
        StringBuilder prettyBuilder = new StringBuilder();
        for (int i = 0; i < children.size(); i++) {
            FormChildComponentModel child = (FormChildComponentModel) children.get(i);
            Attribute typeAttr = child.getAttribute("type");
            String type_clzz = "hs-form-txt";
            String size_clzz = "hs-form-small";

            if( typeAttr != null && !Utils.nullOREmpty( typeAttr.getValue() ) )
            {
                String typeValue = typeAttr.getValue();
                if(typeValue.equalsIgnoreCase("submit") ) {
                    type_clzz = "hs-form-button";
                    size_clzz = "";
                }
            }

            Attribute clzzAttr = child.getAttribute("class");

            if( Utils.nullOREmpty( clzzAttr ) )
            {
                child.addAttribute(new Attribute("class", size_clzz
                        + " "
                        + type_clzz ));
            }
            else
            {
                StringBuilder clzzToAddBuilder = new StringBuilder();
                String oldClass = clzzAttr.getValue();
                if( !oldClass.contains(type_clzz) )
                {
                    clzzToAddBuilder.append(type_clzz);
                    clzzToAddBuilder.append(" ");
                }
                if( oldClass.contains(size_clzz) )
                {
                    clzzToAddBuilder.append(size_clzz);
                    clzzToAddBuilder.append(" ");
                }
                clzzToAddBuilder.trimToSize();
                clzzAttr.setValue( oldClass + " " +clzzToAddBuilder.toString() );
            }
            child.setMode(rowModel.getMode());
            String childString = ComponentProcessor.processComponentContent(child, bindings);
            prettyBuilder.append(childString);
        }
        return prettyBuilder.toString();
    }

    FormMode parseFormMode(List<Attribute> attributes, Object bindings) {
        Attribute attribute = null;
        for (int i = 0; i < attributes.size(); i++) {
            Attribute _attr = attributes.get(i);
            if (_attr.getName().equalsIgnoreCase("mode")) {
                attribute = _attr;
                break;
            }
        }
        if (attribute == null) {
            return FormMode.EDIT;
        }
        String attributeVal = attribute.getValue();
        if (attributeVal.contains("@{")) {
            CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(attributeVal);
            attributeVal = (String) TemplateRuntime.execute(compiledTemplate, bindings);
        }
        return FormMode.parse(attributeVal);
    }

    @Override
    public void createContent(ComponentModel model, Object bindings, OutputStream outputStream) {
        StringBuilder builder = new StringBuilder();

        String labelTagName = "label";

        //get label value5322
        Attribute label = model.getAttribute(labelTagName);
        ComponentTagHandlerFactory tagFactory = ComponentTagHandlerFactory.getInstance();
        String id = Utils.EMPTY_STRING;

        StripesApplicationContext applicationContext =
                model.getComponentTypeStateHelper().getHsContext();

        if (label != null) {
            List<ComponentModel> children = model.getChildren();
            if (!Utils.nullOREmpty(children)) {
                FormChildComponentModel child = (FormChildComponentModel) children.get(0);
                id = child.getId();
            }

            StandardHTMLComponentModel labelModel = new StandardHTMLComponentModel(labelTagName);
            labelModel.addAttribute(new Attribute("for", id));
            labelModel.addAttribute(new Attribute("class", "hs-form-lbl"));
            labelModel.setText(label.getValue());
            //create label

            ComponentTagHandler labelTagHandler = tagFactory.getTagHandlerForTag( new QName(labelTagName), applicationContext);
            builder.append(labelTagHandler.createContent(labelModel, bindings));
        }

        //create input tag
        builder.append(this.processContent(model, bindings));

        //create div wrapping
        builder.trimToSize();

        String divTagName = "div";
        StandardHTMLComponentModel divModel = new StandardHTMLComponentModel(divTagName);
        divModel.addAttribute(new Attribute("class", "grid-12-12"));
        divModel.setText(builder.toString());
        ComponentTagHandler divTagHandler = tagFactory.getTagHandlerForTag(new QName(divTagName), applicationContext );

        divTagHandler.createContent(divModel, bindings, outputStream);
    }
}
